package com.miplus.ccrm.core.service.aiqm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelRulesAddVo;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelRulesDelVo;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelRulesEditVo;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.QmQaMainMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 20144
 * @description 针对表【QM_QA_MAIN(质检项主项表)】的数据库操作Service实现
 * @createDate 2025-08-20 13:33:42
 */
@Service
public class QmQaMainServiceImpl extends ServiceImpl<QmQaMainMapper, QmQaMain>
        implements QmQaMainService {

    @Resource
    private QmQaSubService qaSubService;
    @Resource
    private QmQaMainMapper qmQaMainMapper;
    @Resource
    @Lazy
    private QmModelRulesLinkService modelRulesLinkService;

    @Override
    public CmdResVo qmModelRulesAdd(String usrid, QmModelRulesAddVo paras) {

        QmQaMain def = new QmQaMain();
        BeanUtils.copyProperties(paras, def);
        def.setCrusr(usrid);
        def.setCrdt(new Date());
        def.setMdusr(usrid);
        def.setMddt(new Date());
        int ret = baseMapper.insert(def);
        if (ret <= 0) {
            return new CmdResVo(ret, "新增质检主项失败");
        }
        //新增成功的执行,新增关联表
        Integer templateid = paras.getTemplateid();
        if (templateid != null) {
            QmModelRulesLink link = new QmModelRulesLink();
            link.setTemplateid(templateid);
            link.setQamainsid(def.getQamainsid().intValue());
            modelRulesLinkService.getBaseMapper().insert(link);
        }
        return new CmdResVo(ret, def.getQamainsid().toString());
    }

    /**
     * 修改大模型主项规则
     *
     * @param usrid
     * @param paras
     * @return
     */
    @Override
    public CmdResVo qmModelRulesEdit(String usrid, QmModelRulesEditVo paras) {
        Integer templateid = paras.getTemplateid();
        Long qamainsid = paras.getQamainsid();
        String qamainname = paras.getQamainname();
        String assessscheme = paras.getAssessscheme();
        QmQaMain def = baseMapper.selectById(qamainsid);
        if (def == null) {
            return new CmdResVo(0, "质检主项不存在");
        }
        def.setQamainname(qamainname);
        def.setAssessscheme(assessscheme);
        def.setMdusr(usrid);
        def.setMddt(new Date());
        int ret = baseMapper.updateById(def);
        if (ret <= 0) {
            return new CmdResVo(ret, "修改质检主项失败");
        }
        return new CmdResVo(ret, qamainsid.toString());
    }

    /**
     * 删除大模型主项规则或者子规则，看传递参数
     *
     * @param usrid
     * @param paras
     * @return
     */
    @Override
    public CmdResVo qmModelRulesDel(String usrid, QmModelRulesDelVo paras) {
        Long qamainsid = paras.getQamainsid();
        Long qasubid = paras.getQasubid();
        if (ObjectUtil.isAllNotEmpty(qamainsid, qasubid)) {
            //需要从主规则的列表数组先移除子项，在删除子项
            QmQaMain def = qmQaMainMapper.selectById(qamainsid);
            String qasubids = def.getQasubid(); // 这里存的是JSON数组["1","2","3"]
            if (StrUtil.isNotBlank(qasubids)) {
                // 先移除数组的子项数值
                cn.hutool.json.JSONArray qasubidsArray = JSONUtil.parseArray(qasubids);
                qasubidsArray.remove(qasubid.toString());
                // 更新主规则的列表数组
                def.setQasubid(qasubidsArray.toString());
                qmQaMainMapper.updateById(def);
                qaSubService.removeById(qasubid);
            }
        } else {
            //删除主规则，关联表也删除
            modelRulesLinkService.remove(new QueryWrapper<QmModelRulesLink>().eq("qamainsid", qamainsid));
            baseMapper.deleteById(qamainsid);
        }
        return new CmdResVo(1, qamainsid.toString());
    }

}




